<template>
  <el-form ref="form" :model="form" label-width="120px">
    <el-form-item label="Short Name">
      <el-input v-model="form.name"></el-input>
    </el-form-item>
    <el-form-item label="Unit">
      <el-select v-model="form.region" placeholder="选择Unit">
        <el-option label="kg" value="kg"></el-option>
        <el-option label="km" value="km"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="Display Format">
      <el-input v-model="form.name"></el-input>
    </el-form-item>
    <el-form-item label="Long Name">
      <el-input v-model="form.name"></el-input>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="onSubmit">立即创建</el-button>
      <el-button>取消</el-button>
    </el-form-item>
  </el-form>
  <el-tabs
    v-if="isEmpty === false"
    class="editor"
    :class="{ isEmpty: isEmpty }"
    v-model="editableTabsValue"
    type="card"
    closable
    @tab-remove="removeTab"
  >
    <el-tab-pane
      v-for="(item, index) in editableTabs"
      :key="item.title"
      :label="item.title"
      :name="item.title"
    >
      <editor-tab-vue :editorItem="item" :editorIndex="index"></editor-tab-vue>
    </el-tab-pane>
  </el-tabs>
  <div v-else class="empty">
    <el-empty></el-empty>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, computed } from 'vue'
import { useStore } from '@/render/store/index'
import EditorTabVue from './cmps/editor-tab.vue'

export default defineComponent({
  components: {
    EditorTabVue
  },
  setup() {
    let store = useStore()

    let editableTabs = computed(() => {
      return store.state.base.tabs
    })

    let isEmpty = computed(() => {
      if (editableTabs.value.length > 0) {
        return false
      } else {
        return true
      }
    })

    let editableTabsValue = computed(() => {
      return store.state.base.activeTabName
    })

    const removeTab = (targetName: string) => {
      const deleteTabIndex = editableTabs.value?.findIndex(
        (item) => item.title === targetName
      )
      store.dispatch('base/closePage', deleteTabIndex)
    }

    return {
      isEmpty,
      editableTabs,
      editableTabsValue,
      removeTab
    }
  },
  data() {
    return {
      form: {
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      }
    }
  },
  methods: {
    onSubmit() {
      console.log('submit!')
    }
  }
})
</script>

<style scoped lang="less">
@import '../../assets/style/index';
.editor {
  height: 100%;
  width: 100%;
  .isEmpty {
    display: none;
  }
  & > :deep(.el-tabs__header) {
    position: sticky;
    top: 0;
    z-index: 100;
    width: 100%;
    height: @main-row-height;
    border-bottom: @border-style;
    background-color: @main-background-color;
    margin: 0;
    .el-tabs__nav-wrap.is-scrollable {
      padding: 0 30px;
    }
    .el-tabs__nav-wrap {
      .el-tabs__nav-next,
      .el-tabs__nav-prev {
        width: 30px;
        height: @main-row-height;
        line-height: @main-row-height;
        .el-icon {
          box-sizing: content-box;
          margin: 5px;
          padding: 4px;
        }
        .el-icon:hover {
          background-color: #ccc;
        }
      }
      .el-tabs__nav-scroll {
        .el-tabs__nav {
          border-radius: 0;
          border: 0;
          .el-tabs__item {
            height: @main-row-height;
            line-height: @main-row-height;
          }
          .el-tabs__item:hover {
            background-color: #ccc;
          }
          .el-tabs__item.is-active {
            background-color: @content-background-color;
            border-bottom: 3px #409eff solid;
          }
        }
      }
    }
  }
  & > :deep(.el-tabs__content) {
    max-height: calc(~'100% - @{main-row-height}');
    min-height: calc(~'100% - @{main-row-height}');
    overflow-y: auto;
    background-color: @content-background-color;
  }
}
.empty {
  height: 100%;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
</style>
